package cn.tedu.sims.controller;

import cn.tedu.sims.pojo.dto.RoleDTO;
import cn.tedu.sims.pojo.dto.RolePowerUpdateDTO;
import cn.tedu.sims.pojo.dto.RoleUpdateDTO;
import cn.tedu.sims.pojo.entity.Power;
import cn.tedu.sims.pojo.entity.Role;
import cn.tedu.sims.pojo.entity.User;
import cn.tedu.sims.service.IRoleService;
import cn.tedu.sims.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "01.角色管理模块")
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/add-new")
    @ApiOperation("添加角色")
    @PreAuthorize("hasAuthority('/roles/add-new')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(RoleDTO roleDTO){
        roleService.addNew(roleDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除角色")
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/roles/id/delete')")
    @ApiImplicitParam(name="id",value = "相册id",required = true,dataType = "int")
    public JsonResult<Void> delete(@Range(min = 1,message = "删除失败,ID无效") @PathVariable Long id){
        roleService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("角色列表")
    @GetMapping("")
    @PreAuthorize("hasAuthority('/roles')")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<Role>> list(){
        List<Role> list = roleService.list();
        log.debug("list = {}",list);
        return JsonResult.ok(list);
    }

    @ApiOperation("模糊查询角色")
    @PreAuthorize("hasAuthority('/roles/selectByWd/wd')")
    @PostMapping("/selectByWd/{wd}")
    public JsonResult<List<Role>> selectByWd(@PathVariable String wd){
        List<Role> roles = roleService.selectByWd(wd);
        log.debug("wd = {}",wd);
        log.debug("roles = {}",roles);
        return JsonResult.ok(roles);
    }

    @ApiOperation("根据id查询角色详情")
    @ApiOperationSupport(order = 410)
    @PreAuthorize("hasAuthority('/roles/selectById/id')")
    @GetMapping("/selectById/{id}")
    public JsonResult<Role> selectById(@PathVariable Long id){
        Role role = roleService.getStandardById(id);
        return JsonResult.ok(role);
    }

    @ApiOperation("修改角色")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/roles/update/id')")
    @PostMapping("/update/{id}")
    public JsonResult<Void> update(RoleUpdateDTO roleUpdateDTO){
        roleService.update(roleUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("修改角色的权限")
    @ApiOperationSupport(order = 301)
    @PreAuthorize("hasAuthority('/roles/update/rolePower')")
    @PostMapping("/update/rolePower")
    public JsonResult<Void> updateRolePower(RolePowerUpdateDTO r){
        roleService.updateRolePower(r);
        return JsonResult.ok();
    }

    @ApiOperation("通过角色id查询对应用户")
    @ApiOperationSupport(order = 440)
    @PreAuthorize("hasAuthority('/roles/id/selectByRoleId')")
    @GetMapping("/{id}/selectByRoleId")
    public JsonResult<List<User>> selectByRoleId(@PathVariable Long id){
        List<User> users = roleService.selectByRoleId(id);
        return JsonResult.ok(users);
    }

    @ApiOperation("查询父级权限列表")
    @ApiOperationSupport(order = 700)
    @PreAuthorize("hasAuthority('/roles/power')")
    @GetMapping("/power")
    public JsonResult<List<Power>> selectPower(){
        List<Power> powers = roleService.selectPower();
        return JsonResult.ok(powers);
    }

    @ApiOperation("通过父级ID查询子级权限列表")
    @ApiOperationSupport(order = 701)
    @PreAuthorize("hasAuthority('/roles/power/son/id')")
    @GetMapping("/power/son/{id}")
    public JsonResult<List<Power>> selectPowerSon(@PathVariable Long id){
        List<Power> powers = roleService.selectPowerBySon(id);
        return JsonResult.ok(powers);
    }

    @ApiOperation("查询已有权限列表")
    @ApiOperationSupport(order = 702)
    @PreAuthorize("hasAuthority('/roles/power/exist/id')")
    @GetMapping("/power/exist/{id}")
    public JsonResult<List<Long>> selectPowerExist(@PathVariable Long id){
        List<Long> powers = roleService.selectPowerExist(id);
        return JsonResult.ok(powers);
    }

}
